package cn.kpt.biz.service.impl;

import cn.kpt.beans.pojo.ItripHotel;
import cn.kpt.beans.pojo.ItripHotelOrder;
import cn.kpt.beans.pojo.ItripHotelRoom;
import cn.kpt.beans.pojo.ItripHotelTempStore;
import cn.kpt.beans.vo.order.PreAddOrderVO;
import cn.kpt.biz.service.HotelOrderService;
import cn.kpt.biz.service.HotelRoomService;
import cn.kpt.dao.itriphotel.ItripHotelMapper;
import cn.kpt.dao.itriphotelorder.ItripHotelOrderMapper;
import cn.kpt.dao.itriphotelroom.ItripHotelRoomMapper;
import cn.kpt.dao.itriphoteltempstore.ItripHotelTempStoreMapper;
import cn.kpt.utils.DateUtil;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * @version 1.0
 * @ClassName HotelOrderServiceImpl
 * @Description TODO
 * @Author kangpengtao
 * @Date 2019/10/23 10:55
 */
@Transactional
@Service
public class HotelOrderServiceImpl implements HotelOrderService {
    @Resource
    private ItripHotelMapper itripHotelMapper;
    @Resource
    private ItripHotelRoomMapper itripHotelRoomMapper;
    @Resource
    private HotelRoomService hotelRoomService;
    @Resource
    private ItripHotelOrderMapper itripHotelOrderMapper;
    @Resource
    private ItripHotelTempStoreMapper itripHotelTempStoreMapper;

    @Override
    public void getPreOrderInfo(PreAddOrderVO vo) throws Exception {
        ItripHotel hotel = itripHotelMapper.getItripHotelById(vo.getHotelId());
        vo.setHotelName(hotel.getHotelName());

        ItripHotelRoom room = itripHotelRoomMapper.getItripHotelRoomById(vo.getRoomId());
        vo.setPrice(room.getRoomPrice());

        // 时间工具类
        List<Date> list = DateUtil.getBetweenDates(vo.getCheckInDate(), vo.getCheckOutDate());
        int stock = hotelRoomService.getRoomStock(vo.getRoomId(), list);
        vo.setStore(stock);
    }

    @Override
    public void addHotelOrder(ItripHotelOrder itripHotelOrder) throws Exception {
        // 锁定库存
        List<Date> betweenDates = DateUtil.getBetweenDates(itripHotelOrder.getCheckInDate(), itripHotelOrder.getCheckOutDate());
        // 连续预定
        for(int i = 0; i < itripHotelOrder.getCount(); i++){
            for (Date betweenDate : betweenDates) {
                ItripHotelTempStore tempStore = new ItripHotelTempStore();
                tempStore.setHotelId(itripHotelOrder.getHotelId());
                tempStore.setRoomId(itripHotelOrder.getRoomId());
                tempStore.setRecordDate(betweenDate);
                tempStore.setCreatedBy(itripHotelOrder.getUserId());
                tempStore.setCreationDate(new Date());
                itripHotelTempStoreMapper.insertItripHotelTempStore(tempStore);
            }
        }

        // 添加订单
        itripHotelOrderMapper.insertItripHotelOrder(itripHotelOrder);
    }

    @Override
    public ItripHotelOrder getById(Long orderId) throws Exception {
        return itripHotelOrderMapper.getItripHotelOrderById(orderId);
    }
}
